<!--
 * @Author: lvye
 * @Description:
 * @Date: 2023-07-07 14:52:51
 * @LastEditTime: 2023-07-12 15:08:36
 * @FilePath: \vue3-ts\src\views\login\cpns\login-account.vue
-->
<template>
  <div>
    <el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue"
import { rules } from "../config/account-config"
import { ElForm } from "element-plus"
import LocalCache from "@/utils/cache"
import { useStore } from "vuex"
export default defineComponent({
  setup() {
    const store = useStore()
    const account = reactive({
      name: LocalCache.getCache("name") ?? "",
      password: LocalCache.getCache("password") ?? ""
    })
    const formRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      formRef.value?.validate((valid: boolean) => {
        if (!valid) {
          return
        }
        if (isKeepPassword) {
          // 缓存
          LocalCache.setCache("name", account.name)
          LocalCache.setCache("password", account.password)
        } else {
          LocalCache.deleteCache("name")
          LocalCache.deleteCache("password")
        }
        // 登录逻辑
        store.dispatch("login/accountLoginAction", { ...account })
      })
    }
    return {
      account,
      rules,
      formRef,
      loginAction
    }
  }
})
</script>

<style lang="less"></style>
